package org.autumn.structure.stack;

/**
 * 实现的功能
 * 1.压栈
 * 2.出栈
 * 3.是否为空栈
 * 4.是否为满栈
 * 5.查看栈顶元素
 * 6.查看栈容量
 */
public class ArrayStack<T> {
    /**
     * 栈的大小
     */
    private int maxSize;
    /**
     * 栈的容器
     */
    private T[] stack;
    /**
     * 栈顶的位置
     * -1 代表无数据
     */
    private int top = -1;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        stack = (T[]) new Object[maxSize];
    }

    public void push(T val) {
        if (isFull()) {
            throw new RuntimeException("This stack is full");
        }
        top++;
        stack[top] = val;
    }

    public T pop() {
        if (isEmpty()) {
            throw new RuntimeException("This stack is empty");
        }
        T val = stack[top];
        top--;
        return val;
    }

    public void list() {
        if (isEmpty()) {
            throw new RuntimeException("This stack is empty");
        }
        for (int i = 0; i < stack.length; i++) {
            System.out.println("stack[" + i + "] = " + stack[i]);
        }
    }

    public boolean isFull() {
        return top == maxSize - 1;
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public int getMaxSize(){
        return maxSize;
    }

    public T peek(){
        return stack[top];
    }
}
